import { each } from '../libs/utils';

class RowCol {
    constructor(opts = {}) {
        this.defSize = 40;
        if (opts.defaultSize) {
            this.defSize = opts.defaultSize;
        }

        this.items = {};

        this.totalCount = 0;
        this.totalSize = 0;

        this._maxKey = 0;
        this._minKey = 0;

        this.indexItem = new ItemInfo('-1', {
            size: 40,
            isHeader: true,
            classList: ['colnum']
        })
    }

    setTotalCount(count) {
        this.totalCount = count;
        this.totalSize = this.getRangeSize(0, count - 1);
    }

    _setKeyInfo(key) {
        let k = parseInt(key);
        if (isNaN(k)) {
            console.warn('行列中的index只能为number类型。');
            return;
        }
        this._maxKey = Math.max(k, this._maxKey);
        this._minKey = Math.min(k, this._minKey);
    }

    setData(dataArr = []) {
        let _this = this;
        let items = this.items;
        each(dataArr, (item, index) => {
            let key = item.index === undefined ? index : item.index;
            items[key] = new ItemInfo(key, item);
            _this._setKeyInfo(key);
        })
    }

    getItem(key) {
        let item = this.items[key];
        if (!item) {
            item = new ItemInfo(key);
            this.items[key] = item;
        }
        return item;
    }

    getData(start, end) {
        let range = [];
        let me = this;
        range.push(this.indexItem);
        each(this.items, (oitem, key) => {
            let item = me.getItem(key);
            range.push(item);
        }, {
            start: start,
            end: end
        });
        return range;
    }

    updateAttr(key, attrs) {
        let originItem = this.getItem(key);
        let oSize = originItem.getAttr('size', this.defSize);
        if (attrs.size !== undefined) {
            this.totalSize = this.totalSize + attrs.size - oSize;
        }
        originItem.setAttr(attrs);
    }

    batchUpdateAttr(attrsArr = []) {
        each(attrsArr, (attrs) => {
            let key = attrs.index;
            if (key !== undefined) {
                this.updateAttr(key, attrs);
            }
        })
    }

    getRangeSize(start, end) {
        let me = this;
        let size = 40;
        each(this.items, (oitem, index) => {
            let item = me.getItem(index);
            size += item.getAttr('size', this.defSize);
        }, {
            start: start,
            end: end
        })
        return size;
    }

    getEndBySize(size, start = 0) {
        let index = start;
        size -= 40;
        while (index < this.totalCount && size > 0) {
            let item = this.getItem(index);
            size -= item.getAttr('size', this.defSize);
            index++;
        }
        return index;
    }
}

class ItemInfo {
    constructor(key, info = {}) {
        this.userData = info;

        this.style = info.style;
        this.classList = info.classList;

        this.size = info.size;
        this.index = key;

        this.isHeader = info.isHeader || false;
    }

    setAttr(attrs) {
        for (let key in attrs) {
            this[key] = attrs[key];
        }
    }

    getAttr(attrname, defValue) {
        let value = this[attrname];
        return value === undefined ? defValue : value;
    }
}

export default RowCol;